starter.run(
	function($http, $state, $ionicPlatform, $rootScope, $ionicPopup, $timeout, $ionicLoading, $ionicHistory, $ionicViewSwitcher) {
		//返回，并清除当前页面缓存
		$rootScope.goBack = function(stateName) {
			var historyId = $ionicHistory.currentHistoryId();
			var history = $ionicHistory.viewHistory().histories[historyId];
			var length = history.stack.length;
			// $ionicViewSwitcher.nextDirection('back');
			$ionicHistory.goBack();
			if (stateName) {
				$timeout(function() {
					for (var i = 0; i < length; i++) {
						if (stateName == history.stack[i].stateName) {
							$ionicHistory.clearCache([history.stack[i].stateId]);
						}
					}
				}, 150);
			};
		};

		/*
		 * 根据路由页面跳转
		 * newPage：新页面
		 * param:传递参数
		 * */
		$rootScope.goNewPage = function(newPage, param) {
			// $ionicViewSwitcher.nextDirection('forward');
			$state.go(newPage, param);
		};

		//返回历史栈中的相应位置的页面，并清空靠后页面缓存
		$rootScope.goBackMany = function(depth) {
			// get the right history stack based on the current view
			var historyId = $ionicHistory.currentHistoryId();
			var history = $ionicHistory.viewHistory().histories[historyId];
			// set the view 'depth' back in the stack as the back view
			var targetViewIndex = history.stack.length - 1 - depth;
			$ionicHistory.backView(history.stack[targetViewIndex]);
			// navigate to it
			$ionicHistory.goBack();
			$timeout(function() {
				for (var i = history.stack.length - 1; i > targetViewIndex; i--) {
					$ionicHistory.clearCache([history.stack[i].stateId]);
				}
			}, 200);

		};

		//根据页面stateName返回所在页面的上级页面，并清空靠后页面缓存
		$rootScope.returnToState = function(stateName) {
			var historyId = $ionicHistory.currentHistoryId();
			var history = $ionicHistory.viewHistory().histories[historyId];
			for (var i = history.stack.length - 1; i >= 0; i--) {
				if (history.stack[i].stateName == stateName) {
					$ionicHistory.backView(history.stack[i]);
					$ionicHistory.goBack();
				}
			};
			$timeout(function() {
				for (var i = history.stack.length - 1; i >= 0; i--) {
					if (history.stack[i].stateName != stateName) {
						$ionicHistory.clearCache([history.stack[i].stateId]);
					}
				}
			}, 200);
		};

		//清楚前页面所在栈的缓存，并返回tab页面 （仅在返回tab页面时使用，如：tab.more）
		$rootScope.clearCacheToState = function(tabPage) {
			var historyId = $ionicHistory.currentHistoryId();
			var history = $ionicHistory.viewHistory().histories[historyId];
			$state.go(tabPage);
			$timeout(function() {
				for (var i = history.stack.length - 1; i >= 0; i--) {
					if (history.stack[i].stateName != tabPage) {
						$ionicHistory.clearCache([history.stack[i].stateId]);
					}
				}
			}, 200);
		};


		//主页面返回显示退出提示框
		$ionicPlatform.registerBackButtonAction(function(e) {
			e.preventDefault();
			$rootScope.hideLoading();
			console.log('back')

			function showConfirm() {
				var confirmPopup = $ionicPopup.confirm({
					title: '<center>退出应用?</center>',
					template: '<center>你确定要退出应用吗?<center>',
					okText: '退出',
					okType: 'pop-buttons-right',
					cancelText: '取消',
					cancelType: 'pop-buttons-left',
				});
				confirmPopup.then(function(res) {
					if (res) {
						ionic.Platform.exitApp();
					} else {
						// Don't close
					}
				});
			}

			if ($ionicHistory.currentView().stateName == "tab.shouye" || $ionicHistory.currentView().stateName == "login") {
				showConfirm();
			} else if ($ionicHistory.currentView().stateName == "tab.mine") {
				$state.go("tab.shouye");
			} else {
				$ionicHistory.goBack();
			}
			return false;
		}, 101);

	});